package psn.kiko.service.business.associated;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import psn.kiko.constant.DBConstant;
import psn.kiko.mapper.business.associated.UserRoleMapper;
import psn.kiko.pojo.dto.UserRoleDTO;
import psn.kiko.pojo.entity.system.UserRoleEntity;
import psn.kiko.pojo.vo.AllocateRoleVo;
import psn.kiko.pojo.vo.InterfaceVO;

import java.util.ArrayList;
import java.util.List;

/**
 * user_role关联表服务
 */
@Service
public class UserRoleService{
	@Autowired
	UserRoleMapper userRoleMapper;
	
	/**
	 * 为一个角色分配一批用户
	 */
	@Transactional
	public Integer allocateMultiUsersToOneRole(Integer roleId,List<Integer> userIds){
		ArrayList<UserRoleDTO> list = new ArrayList<>();
		for(Integer userId: userIds){
			list.add(new UserRoleDTO(userId,roleId));
		}
		return userRoleMapper.batchInsert(list);
	}
	
	/**
	 * 为一个用户分配一批角色
	 */
	@Transactional
	public Integer allocateOneUserToMultiRoles(Integer userId,List<Integer> roleIds){
		ArrayList<UserRoleDTO> list = new ArrayList<>();
		for(Integer roleId: roleIds){
			list.add(new UserRoleDTO(userId,roleId));
		}
		return userRoleMapper.batchInsert(list);
	}
	
	/**
	 * 根据角色主键批量删除记录
	 */
	@Transactional
	public Integer batchDeleteByRoleIds(List<Integer> roleIds){
		return userRoleMapper.batchDelete(null,roleIds);
	}
	
	/**
	 * 根据用户主键批量删除记录
	 */
	@Transactional
	public Integer batchDeleteByUserIds(List<Integer> userIds){
		return userRoleMapper.batchDelete(userIds,null);
	}
	
	/**
	 * 批量启用用某一角色下的某一批用户
	 */
	@Transactional
	public Integer batchEnableByRoleId(Integer roleId,List<Integer> userIds){
		ArrayList<Integer> roleIds = new ArrayList<>();
		userIds.add(roleId);
		return userRoleMapper.batchUpdate(userIds,roleIds,DBConstant.ENABLE);
	}
	
	/**
	 * 批量禁用某一角色下的某一批用户
	 */
	@Transactional
	public Integer batchDisableByRoleId(Integer roleId,List<Integer> userIds){
		ArrayList<Integer> roleIds = new ArrayList<>();
		userIds.add(roleId);
		return userRoleMapper.batchUpdate(userIds,roleIds,DBConstant.DISABLE);
	}
	
	/**
	 * 禁用某一用户的一批角色
	 */
	@Transactional
	public Integer batchDisableRolesByNavigationId(List<Integer> roleIds,Integer userId){
		ArrayList<Integer> userIds = new ArrayList<>();
		userIds.add(userId);
		return userRoleMapper.batchUpdate(userIds,roleIds,DBConstant.DISABLE);
	}
	
	/**
	 * 启用某一用户的一批角色
	 */
	@Transactional
	public Integer batchEnableRolesByNavigationId(List<Integer> roleIds,Integer userId){
		ArrayList<Integer> userIds = new ArrayList<>();
		userIds.add(userId);
		return userRoleMapper.batchUpdate(userIds,roleIds,DBConstant.ENABLE);
	}
	
	/**
	 * 根据用户id获取其已分配的角色信息
	 */
	public List<AllocateRoleVo> selectAllocatedRolesByOfUserId(Integer userId){
		return userRoleMapper.selectAllocatedRolesByUserId(userId);
	}
	
	/**
	 * 获取用户还未分配到的角色
	 */
	public List<AllocateRoleVo> getUnallocatedRolesByUserId(Integer userId){
		return userRoleMapper.selectUnallocatedRolesByUserId(userId);
	}
	
	/**
	 * 根据用户id和角色id集合批量删除
	 */
	public Integer batchDeleteByUserIdAndRoleIds(Integer userId,List<Integer> roleIds){
		ArrayList<Integer> userIds = new ArrayList<>();
		userIds.add(userId);
		return userRoleMapper.batchDelete(userIds,roleIds);
	}
	
	/**
	 * 批量启用用户的一批角色
	 */
	public Integer baEnableByUserId(List<Integer> roleIds,Integer userId){
		ArrayList<Integer> userIds = new ArrayList<>();
		userIds.add(userId);
		return userRoleMapper.batchUpdate(userIds,roleIds,DBConstant.ENABLE);
	}
	
	/**
	 * 批量禁用用户的一批角色
	 */
	public Integer batchDisableByUserId(List<Integer> roleIds,Integer userId){
		ArrayList<Integer> userIds = new ArrayList<>();
		userIds.add(userId);
		return userRoleMapper.batchUpdate(userIds,roleIds,DBConstant.DISABLE);
	}
	
	/**
	 * 获取用户已分配的API
	 */
	public List<InterfaceVO> getAllAllocatedApisOfUser(Integer userId){
		return userRoleMapper.selectInterfacesByUserId(userId);
	}
	
	public UserRoleEntity getByUserIdAndRoleId(Integer userId,Integer roleId){
		return userRoleMapper.selectByUserIdAndRoleId(userId,roleId);
	}
}
